from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
from catboost import Pool, CatBoostRegressor
import pandas as pd
import datetime
from sklearn import metrics
import warnings
warnings.filterwarnings('ignore')
from sklearn.model_selection import StratifiedKFold
#warnings.filterwarnings('ignore')
#%matplotlib inline
from sklearn.metrics import roc_auc_score
## 数据降维处理的
from sklearn.model_selection import train_test_split
from catboost import CatBoostClassifier
train_data = pd.read_csv('new_train_data_FE8.csv',index_col='id')
train_label = train_data.pop("isDefault")
test_data = pd.read_csv('new_test_data_FE8.csv',index_col='id')
train_data = train_data.drop(["issueDate","earliesCreditLine"],axis=1)
test_data = test_data.drop(["issueDate","earliesCreditLine"],axis=1)
Cat_cols = ["term","grade","subGrade","employmentLength","homeOwnership","verificationStatus","postCode","regionCode","delinquency_2years","pubRec","openAcc","totalAcc","pubRecBankruptcies","initialListStatus","applicationType","n0","n1","n2","n4","n5","n6","n7","n8","n9","n10","n11","n12","n13","n14","employmentTitle_len","title_len","purpose","ficoRange","ficoRangeLow","num_years","ficoRangeHigh","issueDate_now_year","earliesCreditLine_now_year","employmentTitle","title","num_years","loanAmnt","interestRate"]
train_data[Cat_cols] = train_data[Cat_cols].astype(str)
test_data[Cat_cols] = test_data[Cat_cols].astype(str)

X_train,X_val,y_train,y_val  = train_test_split(train_data,train_label,test_size=0.1,random_state=0)

# initialize Pool
#建模用本地验证
# train_pool = Pool(X_train,
#                   y_train,
#                   cat_features=Cat_cols)
# val_pool = Pool(X_val,
#                   y_val,
#                   cat_features=Cat_cols)

#全部训练集和测试集
total_train_pool = Pool(train_data,
                  train_label,
                  cat_features=Cat_cols)
test_pool = Pool(test_data,
                 cat_features=Cat_cols)

# specify the training parameters
model = CatBoostClassifier(iterations=500,  #修改部分原8000
                        learning_rate=0.01,
                        depth=None,
                        l2_leaf_reg=5,
                        model_size_reg=None,
                        rsm=None,
                        loss_function="Logloss",
                        border_count=None,
                        feature_border_type=None,
                        per_float_feature_quantization=None,
                        input_borders=None,
                        output_borders=None,
                        fold_permutation_block=None,
                        od_pval=None,
                        od_wait=None,
                        od_type="Iter",
                        nan_mode=None,
                        counter_calc_method=None,
                        leaf_estimation_iterations=None,
                        leaf_estimation_method=None,
                        thread_count=None,
                        random_seed=547,
                        #全集为False
                        use_best_model=False,
                        best_model_min_trees=None,
                        verbose=None,
                        silent=None,
                        logging_level=None,
                        metric_period=500,
                        ctr_leaf_count_limit=None,
                        store_all_simple_ctr=None,
                        max_ctr_complexity=None,
                        has_time=None,
                        allow_const_label=None,
                        one_hot_max_size=None,
                        random_strength=None,
                        name=None,
                        ignored_features=None,
                        train_dir=None,
                        custom_metric=None,
                        eval_metric="AUC",
                        bagging_temperature=None,
                        save_snapshot=None,
                        snapshot_file=None,
                        snapshot_interval=None,
                        fold_len_multiplier=None,
                        used_ram_limit=None,
                        gpu_ram_part=None,
                        pinned_memory_size=None,
                        allow_writing_files=None,
                        final_ctr_computation_mode=None,
                        approx_on_full_history=None,
                        boosting_type=None,
                        simple_ctr=None,
                        combinations_ctr=None,
                        per_feature_ctr=None,
                        ctr_target_border_count=None,
                        task_type=None,
                        device_config=None,
                        devices=None,
                        bootstrap_type=None,
                        subsample=None,
                        sampling_unit=None,
                        dev_score_calc_obj_block_size=None,
                        max_depth=12,
                        n_estimators=None,
                        num_boost_round=None,
                        num_trees=None,
                        colsample_bylevel=0.7,
                        random_state=None,
                        reg_lambda=None,
                        objective=None,
                        eta=None,
                        max_bin=254,
                        gpu_cat_features_storage=None,
                        data_partition=None,
                        metadata=None,
                        early_stopping_rounds=500,
                        cat_features=Cat_cols,
                        grow_policy=None,
                        min_data_in_leaf=250,
                        min_child_samples=None,
                        max_leaves=None,
                        num_leaves=None,
                        score_function=None,
                        leaf_estimation_backtracking=None,
                        ctr_history_unit=None,
                        monotone_constraints=None,
                        feature_weights=None,
                        penalties_coefficient=None,
                        first_feature_use_penalties=None,
                        model_shrink_rate=None,
                        model_shrink_mode=None,
                        langevin=None,
                        diffusion_temperature=None,
                        boost_from_average=None)
model.fit(total_train_pool)
y_pred=model.predict_proba(X_train)[:,1]
""""预测并计算roc的相关指标"""
fpr, tpr, threshold = metrics.roc_curve(y_train, y_pred)
roc_auc = metrics.auc(fpr, tpr)
print('Train'+'AUC：{}'.format(roc_auc))

y_pred=model.predict_proba(X_val)[:,1]
""""预测并计算roc的相关指标"""
fpr, tpr, threshold = metrics.roc_curve(y_val, y_pred)
roc_auc = metrics.auc(fpr, tpr)
print('Validation'+'AUC：{}'.format(roc_auc))

